﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Services.Client;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Demo.TableStorageDataServices.SL.DemoDataService;

namespace Demo.TableStorageDataServices.SL
{
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        private readonly ObservableCollection<Enigma> _enigmas = new ObservableCollection<Enigma>();
        public ObservableCollection<Enigma> Enigmas { get { return _enigmas; } }
        
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += (sender, e) =>
                               {
                                   this.DataContext = this;
                                   this.LoadData();
                               };
        }

        public void LoadData()
        {
            var container = new Container(new Uri("http://wwzeclouddemo100511.cloudapp.net/Services/DemoDataService.svc/"));
            //var container = new DemoDataService.Container(new Uri("http://127.0.0.1:81/Services/DemoDataService.svc/", UriKind.Absolute));
            var query = container.Enigmas;

            DataServiceCollection<Enigma> enigmas = new DataServiceCollection<Enigma>();

            

            enigmas.LoadCompleted += (sender, e) =>
                                         {
                                             if (e.Error == null)
                                             {
                                                 if (enigmas.Continuation != null)
                                                 {
                                                     enigmas.LoadNextPartialSetAsync();
                                                 }
                                                 else
                                                 {
                                                     this.DataContext = enigmas;
                                                 }
                                             }
                                         };
            enigmas.LoadAsync(query);
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
